package com.helper.dream.a361phonehelper.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.helper.dream.a361phonehelper.R;
import com.helper.dream.a361phonehelper.bean.TuijianJingpinEntity;
import com.helper.dream.a361phonehelper.utils.L;

import java.util.List;

/**
 * Created by LamYu on 16/12/20/0020.
 */

public class TuijianJingpinAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<TuijianJingpinEntity.DataBeanX.FixedBean> data;
    LayoutInflater inflater;

    public TuijianJingpinAdapter(Context context, TuijianJingpinEntity data) {
        this.context = context;
        this.data = data.getData().getFixed();
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        L.d("viewtype:" + viewType);
        switch (viewType) {
            case 0:
                holder = new MyViewHolerOne(inflater.inflate(R.layout.item_tj_jp_1, parent, false));
                break;
            case 1:
                holder = new MyViewHolerTwo(inflater.inflate(R.layout.item_tj_jp_2, parent, false));
                break;
//            case 2:
//                holder = new MyViewHolderTop(inflater.inflate(R.layout.item_tj_jp_top, parent, false));
//                break;
//            case 3:
//                holder = new MyViewHolderCard(inflater.inflate(R.layout.item_tj_jp_card, parent, false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolerOne) {
            MyViewHolerOne myholder = (MyViewHolerOne) holder;
            TuijianJingpinEntity.DataBeanX.FixedBean bean = data.get(position);
            Glide.with(context).load(bean.getLogo_url_160()).into((myholder).iv);
            myholder.tv_title.setText(bean.getName());
            myholder.tv_num.setText(bean.getDownload_times());
            myholder.tv_size.setText(bean.getSize());
            myholder.tv_word.setText(bean.getSingle_word());
        } else if (holder instanceof MyViewHolerTwo) {
            MyViewHolerTwo myholder = (MyViewHolerTwo) holder;
            TuijianJingpinEntity.DataBeanX.FixedBean bean = data.get(position);
            Glide.with(context).load(bean.getLogo_url_160()).into(myholder.iv);
            Glide.with(context).load(bean.getContentInfo().getImage_url()).into(myholder.image);
            myholder.tv_title.setText(bean.getName());
            myholder.tv_num.setText(bean.getDownload_times());
            myholder.tv_size.setText(bean.getSize());
            myholder.tv_contenttitle.setText(bean.getContentInfo().getTitle());
            myholder.tv_contentdes.setText(bean.getContentInfo().getDes());
        }
//        else if (holder instanceof MyViewHolderTop) {
//            MyViewHolderTop myholder = (MyViewHolderTop) holder;
//            List<TuijianJingpinEntity.DataBeanX.CardListBean> bean = data.getData().getCard_list();
//            Glide.with(context).load(bean.get(0).getEvent_items().get(0).get(0).getBanner()).into(myholder.iv1);
//            Glide.with(context).load(bean.get(0).getEvent_items().get(0).get(1).getBanner()).into(myholder.iv2);
//            Glide.with(context).load(bean.get(0).getEvent_items().get(1).get(0).getBanner()).into(myholder.iv3);
//            Glide.with(context).load(bean.get(0).getEvent_items().get(1).get(1).getBanner()).into(myholder.iv4);
//            myholder.tv_title1.setText(bean.get(0).getEvent_items().get(0).get(0).getTitle());
//            myholder.tv_title2.setText(bean.get(0).getEvent_items().get(0).get(1).getTitle());
//            myholder.tv_title3.setText(bean.get(0).getEvent_items().get(1).get(0).getTitle());
//            myholder.tv_title4.setText(bean.get(0).getEvent_items().get(1).get(1).getTitle());
//            myholder.tv_summary1.setText(bean.get(0).getEvent_items().get(0).get(0).getSummry());
//            myholder.tv_summary2.setText(bean.get(0).getEvent_items().get(0).get(1).getSummry());
//            myholder.tv_summary3.setText(bean.get(0).getEvent_items().get(1).get(0).getSummry());
//            myholder.tv_summary4.setText(bean.get(0).getEvent_items().get(1).get(1).getSummry());
//        }else if (holder instanceof MyViewHolderCard){
//            MyViewHolderCard myholder= (MyViewHolderCard) holder;
//            List<TuijianJingpinEntity.DataBeanX.CardListBean> bean = data.getData().getCard_list();
//            Glide.with(context).load(bean.get(position-data.getData().getFixed().size()).getReserves().get(0).getLogo_url()).into(myholder.iv);
//            myholder.name.setText(bean.get(position-data.getData().getFixed().size()).getName());
//            myholder.name2.setText(bean.get(position-data.getData().getFixed().size()).getReserves().get(0).getName());
//
//        }
    }

    @Override
    public int getItemViewType(int position) {
        if (data!= null) {
            if (data.get(position).getContentInfo() == null) {
                return 0;
            } else {
                return 1;
            }
        }
//        else if (data.getData().getCard_list() != null) {
//            if (data.getData().getCard_list().get(position).getShow_top() == 1) {
//                return 2;
//            } else {
//                return 3;
//            }
//        }
        return 1;
    }

    @Override
    public int getItemCount() {
//        int count = data.getData().getFixed().size()+data.getData().getCard_list().size();
        return data.size();
    }

    public class MyViewHolerOne extends RecyclerView.ViewHolder {
        ImageView iv;
        TextView tv_title, tv_num, tv_size, tv_word;

        public MyViewHolerOne(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv_tj_jp_1);
            tv_title = (TextView) itemView.findViewById(R.id.tv_tj_jp_title_1);
            tv_num = (TextView) itemView.findViewById(R.id.tv_tj_jp_num_1);
            tv_size = (TextView) itemView.findViewById(R.id.tv_tj_jp_size_1);
            tv_word = (TextView) itemView.findViewById(R.id.tv_tj_jp_word_1);
        }
    }

    public class MyViewHolerTwo extends RecyclerView.ViewHolder {
        ImageView iv, image;
        TextView tv_title, tv_num, tv_size, tv_contenttitle, tv_contentdes;

        public MyViewHolerTwo(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv_tj_jp_2);
            image = (ImageView) itemView.findViewById(R.id.iv_tj_jp_image_2);
            tv_title = (TextView) itemView.findViewById(R.id.tv_tj_jp_title_2);
            tv_num = (TextView) itemView.findViewById(R.id.tv_tj_jp_num_2);
            tv_size = (TextView) itemView.findViewById(R.id.tv_tj_jp_size_2);
            tv_contenttitle = (TextView) itemView.findViewById(R.id.tv_tj_jp_contenttitle_2);
            tv_contentdes = (TextView) itemView.findViewById(R.id.tv_tj_jp_contentdes_2);
        }
    }

    public class MyViewHolderTop extends RecyclerView.ViewHolder {
        TextView tv_title1, tv_summary1, tv_title2, tv_summary2, tv_title3, tv_summary3, tv_title4, tv_summary4;
        ImageView iv1, iv2, iv3, iv4;

        public MyViewHolderTop(View itemView) {
            super(itemView);
            tv_title1 = (TextView) itemView.findViewById(R.id.tv_tj_jp_title_top_1);
            tv_title2 = (TextView) itemView.findViewById(R.id.tv_tj_jp_title_top_2);
            tv_title3 = (TextView) itemView.findViewById(R.id.tv_tj_jp_title_top_3);
            tv_title4 = (TextView) itemView.findViewById(R.id.tv_tj_jp_title_top_4);
            tv_summary1 = (TextView) itemView.findViewById(R.id.tv_tj_jp_summary_top_1);
            tv_summary2 = (TextView) itemView.findViewById(R.id.tv_tj_jp_summary_top_2);
            tv_summary3 = (TextView) itemView.findViewById(R.id.tv_tj_jp_summary_top_3);
            tv_summary4 = (TextView) itemView.findViewById(R.id.tv_tj_jp_summary_top_4);
            iv1 = (ImageView) itemView.findViewById(R.id.iv_tj_jp_top_1);
            iv2 = (ImageView) itemView.findViewById(R.id.iv_tj_jp_top_2);
            iv3 = (ImageView) itemView.findViewById(R.id.iv_tj_jp_top_3);
            iv4 = (ImageView) itemView.findViewById(R.id.iv_tj_jp_top_4);
        }
    }

    public class MyViewHolderCard extends RecyclerView.ViewHolder {
        TextView name, name2;
        ImageView banner, iv;
        Button btn;

        public MyViewHolderCard(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.tv_tj_jp_card_name);
            name2 = (TextView) itemView.findViewById(R.id.tv_tj_jp_card_name2);
            banner = (ImageView) itemView.findViewById(R.id.iv_tj_jp_card_banner);
            iv = (ImageView) itemView.findViewById(R.id.iv_tj_jp_card_1);
            btn = (Button) itemView.findViewById(R.id.btn_tj_jp_card);
        }
    }
}
